﻿using System;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;

namespace ALinq.Mapping
{
    ///<summary>
    ///Represents ALinq FluentMappingSource related errors that occur in the this liberary. 
    ///</summary>
    public class FluentMappingException : Exception
    {
        private string message;

        internal FluentMappingException(Expression<Func<string>> msgExpr, params object[] args)
        {
            Debug.Assert(msgExpr.Body.NodeType == ExpressionType.MemberAccess);
            var member = (PropertyInfo)((MemberExpression)msgExpr.Body).Member;
            Name = member.Name;
            var str = (string)member.GetValue(null, null);
            this.message = string.Format(str, args);
        }

        ///<summary>
        ///Message of this exception.
        ///</summary>
        public override string Message
        {
            get
            {
                return message;
            }
        }

        ///<summary>
        /// Name of this exception.
        ///</summary>
        public string Name { get; private set; }
    }
}